package com.jia.together.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jia.together.entity.Category;
import com.jia.together.service.CategoryService;
import com.jia.together.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.rmi.runtime.Log;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dujiajia
 * @since 2021-02-19
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public R list(@RequestParam Long pageNum,@RequestParam Long pageSize){
        Page<Category> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");
        wrapper.orderByDesc("sort");
        Page<Category> categoryPage = categoryService.page(page, wrapper);
        List<Category> records = categoryPage.getRecords();
        long total = categoryPage.getTotal();
        Map<String,Object> map = new HashMap<>();
        map.put("records",records);
        map.put("total",total);
        return R.ok().data(map);
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable String id){
        Category byId = categoryService.getById(id);
        return R.ok().data("data",byId);
    }

    @PostMapping("/add")
    public R add(@RequestBody Category category){
        categoryService.save(category);
        return R.ok();
    }
    @PutMapping("/update")
    public R update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.ok();
    }
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable String id){
        categoryService.removeById(id);
        return R.ok();
    }
}

